<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class QuestionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'subject'  => 'required', //unique参数 第一个哪个表 第二个 哪个字段名
            'sections'   => 'required',
            'type'     => 'required',
            'question' => 'required|min:5',
            'answer'   => 'required',
            //
        ];
    }

    public function messages()
    {
        return [
            'subject.required'  => '请选择科目,请选择科目！',
            'sections.required'   => '请选择章节，如果章节为空，请先添加章节',
            'type.required'     => '请选择题型，题型不能为空',
            'question.required' => '问题标题不可以为空',
            'question.min'      => '问题标题至少5个字符',
            'answer.required'   => '正确答案不可以是空的',
        ];
    }

    public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'message' => $validator->errors()->first(),
        ], 422)));
    }
}
